//! @Author: DengLibin
//! @Date: Create in 2024-06-14 13:50:57
//! @Description:

use std::{fs::File, io::Read};

use docx_rs::{read_docx, DocumentChild, Paragraph, ParagraphChild, RunChild, Table, TableCellContent, TableChild, TableRowChild};

use crate::common_result::{to_common_result, CommonResult};

pub fn read_docx_content(file_path: &str) -> CommonResult<()> {
    // Open the DOCX file
    let mut file = to_common_result(File::open(file_path))?;

    let mut buf = vec![];
    to_common_result(file.read_to_end(&mut buf))?;

    // Parse the DOCX file
    let docx = to_common_result(read_docx(&buf))?;
    let children = docx.document.children;
    for i in children {
        
        match i {
            DocumentChild::Paragraph(s) => {
                print_paragraph(s);
            }
            DocumentChild::Table(s) => {
                print_table(s);
            }
            DocumentChild::BookmarkStart(s) => {
                println!("s3 => {:?}\n", s);
            }
            DocumentChild::BookmarkEnd(s) => {
                println!("s4 => {:?}\n", s);
            }
            DocumentChild::CommentStart(s) => {
                println!("s5 => {:?}\n", s);
            }
            DocumentChild::CommentEnd(s) => {
                println!("s6 => {:?}\n", s);
            }
            DocumentChild::StructuredDataTag(s) => {
                println!("s7 => {:?}\n", s);
            }
            DocumentChild::TableOfContents(s) => {
                println!("s8 => {:?}\n", s);
            }
        }
    }

    Ok(())
}

/// @Author: DengLibin
/// @Date: Create in 2024-06-14 14:18:34
/// @Description: 打印段落
fn print_paragraph(s: Box<Paragraph>) {
    // println!("s1 => {:?}", s);
    for ele in s.children {
        match ele {
            ParagraphChild::Run(r) => {
                for ele2 in r.children {
                    match ele2 {
                        RunChild::Text(t) => {
                            println!("{}", t.text);
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
    }
}

/// @Author: DengLibin
/// @Date: Create in 2024-06-14 14:19:55
/// @Description: 打印表格
fn print_table(s: Box<Table>) {
    // println!("s2 => {:?}\n", s);
    for row in s.rows { //遍历行
        match row { //行
            TableChild::TableRow(tr) => {
                for trc in tr.cells { //遍历列
                    match trc {
                        TableRowChild::TableCell(tc) => {
                            for tcc in tc.children { //单元格中的元素
                                match tcc {
                                    TableCellContent::Paragraph(tp) => { //段落
                                        print_paragraph(Box::new(tp));
                                    }
                                    TableCellContent::Table(tt) => { //表格
                                        print_table(Box::new(tt)); //递归
                                        
                                    },
                                    _=>{}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
